function oat = osl_run_oat(oat)

% oat = osl_run_oat(oat)
%
% Runs an OAT (OHBA's easy Analysis Tool) using the settings contained in
% oat, which needs to be setup by calling oat=osl_setup_oat(S);
%
% MWW 2011

global OSLDIR;  

% make dir for the results: 

if(isempty(findstr(oat.source_recon.dirname, '.oat')))
    oat.source_recon.dirname=[oat.source_recon.dirname, '.oat']; 
end;
mkdir(oat.source_recon.dirname);

oat = osl_check_oat(oat);

% turns figure plotting on or off in SPM calls
spm_get_defaults('cmdline',~oat.do_plots);

try,
    if(oat.to_do(1)),

        if(strcmp(oat.source_recon.method,'none')),
            results_fnames=osl_run_source_recon_sensorspace(oat); 
        else(strcmp(oat.source_recon.method,'beamform')),
            results_fnames=osl_run_source_recon_inverse(oat);  % changed by DM
%         else
%             error('recon_method is invalid');
        end;

        % for those that have just been run, overwrite results_fnames:
        oat.source_recon.results_fnames(oat.source_recon.sessions_to_do)=results_fnames(oat.source_recon.sessions_to_do);

        % save OAT to disk
        oat = osl_save_oat(oat);

    end;
catch ME,
    disp('Source recon OAT stage has failed');
    ME.getReport
    return;
end;

try,
    if(oat.to_do(2)),

        try,
            tmp=oat.source_recon.results_fnames;
        catch,
            error('Source recon stage has not been run. To load in a previous oat, try running "oat=osl_load_oat(oat);" and then try this again.');
        end;

        results_fnames = osl_run_first_level(oat); 

        % for those that have just been run, overwrite results_fnames:
        oat.first_level.results_fnames(oat.first_level.sessions_to_do)=results_fnames(oat.first_level.sessions_to_do);
        
        % save OAT to disk
        oat = osl_save_oat(oat);

    end;
catch ME,    
    disp('First level OAT stage has failed');
    ME.getReport
    return;
end; 

try,
    if(oat.to_do(3)), 

        try,
            tmp = oat.first_level.results_fnames;
        catch,
            error('First level stage has not been run. To load in a previous oat, try running "oat=osl_load_oat(oat);" and then try this again.');
        end;

        oat.subject_level.results_fnames = osl_run_subject_level(oat);

        % save OAT to disk
        oat = osl_save_oat(oat);
    end;
catch ME,
    disp('Subject level OAT stage has failed');
    ME.getReport
    return;
end;

try,
    if(oat.to_do(4)), 

        try,
            tmp = oat.subject_level.results_fnames;
        catch,
            error('Subject level stage has not been run. To load in a previous oat, try running "oat=osl_load_oat(oat);" and then try this again.');
        end;

        oat.group_level.results_fnames = osl_run_group_level(oat);

        % save OAT to disk
        oat = osl_save_oat(oat);
    end;
catch ME,
    disp('Group level OAT stage has failed');
    ME.getReport
    return;
end;

%%%%%%%%%%%%%%%%%
%% ICA % HL Mod 1.6 New ICA code 

if(length(oat.to_do)>4)
    if(oat.to_do(5)),

        try,
            tmp=oat.source_recon.results_fnames;
        catch,
                error('Source recon stage has not been run. To load in a previous oat, try running "oat=osl_load_oat(oat);" and then try this again.');
        end;

        oat.ica.results_fnames = osl_ica(oat);
        oat = osl_save_oat(oat);    
    end;
end;